
//
//  B_GZZXNViewController.m
//  Estate
//
//  Created by heyong on 16/6/16.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "B_GZZXNViewController.h"
#import "B_addWLCell_1.h"
#import "addBtnCell.h"
@interface B_GZZXNViewController ()
{
    NSMutableArray *_showKeyArr;
}
@end

@implementation B_GZZXNViewController
//懒加载
- (NSMutableArray *)dataArray{
    if(_dataArray==nil){
        _dataArray=[NSMutableArray array];
       
    }
    return  _dataArray;
}

- (TPKeyboardAvoidingTableView *)tableView{
    if(_tableView==nil){
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRECTS(0, 0, 375, 667)];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView=[[UIView alloc]init];
        [_tableView registerClass:[B_addWLCell_1 class] forCellReuseIdentifier:@"addCell"];
        [_tableView registerClass:[addBtnCell class] forCellReuseIdentifier:@"addbtn"];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"旁站-混凝土灌注桩-物料信息";
    self.num=0;
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.tableView];
    //添加右上方提交按钮
    if(self.isEdit){
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wancheng"] style:UIBarButtonItemStyleDone target:self action:@selector(clickSave)];
    }

    [self setData];
    
}
//!!!!:保存视图
- (void)clickSave{
    
    if(_site){
        NSMutableArray * passOnArr=[NSMutableArray array];
        for (int i=0; i<self.dataArray.count; i++) {
            //模型转字典
            NSDictionary *dic = [self.dataArray[i] yy_modelToJSONObject];
            [passOnArr addObject:dic];
        }
        
        NSMutableDictionary *dics=[NSMutableDictionary dictionary];
        
        for(int i=0;i<passOnArr.count;i++){
            NSMutableDictionary *tempDic=[NSMutableDictionary dictionary];
            NSDictionary *dic=[passOnArr objectAtIndex:i];
            [tempDic setObject:dic[@"chep"] forKey:@"车牌号"];
            [tempDic setObject:dic[@"yuns"] forKey:@"运输单号"];
            [tempDic setObject:dic[@"luod"] forKey:@"实测坍落度mm"];
            [tempDic setObject:dic[@"chug"] forKey:@"出罐温度(冬施)"];
            [tempDic setObject:dic[@"is_1"] forKey:@"随车质保资料是否齐全"];
            [tempDic setObject:dic[@"is_2"] forKey:@"是否合格"];
            [tempDic setObject:dic[@"beiz"] forKey:@"备注"];
            [dics setValue:tempDic forKey:dic[@"number"]];
        }

        self.dataBlock(dics);
        
    }else{
        
        NSMutableArray * passOnArr=[NSMutableArray array];
        
        for (int i=0; i<self.dataArray.count; i++) {
            //模型转字典
            NSDictionary *dic = [self.dataArray[i] yy_modelToJSONObject];
            
            [passOnArr addObject:dic];
        }
        
        NSMutableDictionary *dics=[NSMutableDictionary dictionary];
        
        for(int i=0;i<passOnArr.count;i++){
            NSMutableDictionary *tempDic=[NSMutableDictionary dictionary];
            NSDictionary *dic=[passOnArr objectAtIndex:i];
            [tempDic setObject:dic[@"chep"] forKey:@"车牌号"];
            [tempDic setObject:dic[@"yuns"] forKey:@"运输单号"];
            [tempDic setObject:dic[@"luod"] forKey:@"实测坍落度mm"];
            [tempDic setObject:dic[@"chug"] forKey:@"出罐温度(冬施)"];
            [tempDic setObject:dic[@"is_1"] forKey:@"随车质保资料是否齐全"];
            [tempDic setObject:dic[@"is_2"] forKey:@"是否合格"];
            [tempDic setObject:dic[@"beiz"] forKey:@"备注"];
            
            [dics setObject:tempDic forKey:dic[@"number"]];
        }
        
        self.dataBlock(dics);
    }
    
    

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setData{
    WEAKSELF
    //查看页
    if(_site){
        _showKeyArr=[NSMutableArray array];
        //循环遍历、找到显示的视图。
        for(int i=1;i<=10;i++){
            NSString *key=[NSString stringWithFormat:@"序号%d是否显示",i];
            NSString *value=[_site.wl_2 objectForKey:key];
            //存入所有要显示的key
            if([value isEqualToString:@"是"]){
                [_showKeyArr addObject:[NSString stringWithFormat:@"序号%d",i]];
            }
        }
        //取到显示视图的值 根据显示的key
        for (NSString *key in _showKeyArr) {
            Site_04_01 *site=[Site_04_01 siteWithDict:_site.wl_2[key] AndNumber:key];
            [weakSelf.dataArray addObject:site];
        }
        
        //新增页
    }else{
        
        
    }
    
}


//addDataDic 默认为@""
- (void)addDataDefaine:(long)l{
    NSDictionary *dic=@{@"运输单号":@"",@"备注":@"",@"实测坍落度mm":@"",@"出罐温度(冬施)":@"",@"随车质保资料是否齐全":@"是",@"是否合格":@"是",@"车牌号":@""};
    Site_04_01 *site=[Site_04_01 siteWithDict:dic AndNumber:[NSString stringWithFormat:@"序号%li",l]];
    [self.dataArray addObject:site];
}

#pragma  mark-UITableViewDelegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==self.dataArray.count){
        return 40*BIZ;
    }
    return 360*BIZ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    typeof(self) __weak weakSelf = self;
    //显示添加按钮
    if(indexPath.row==self.dataArray.count){
        addBtnCell *cell=[tableView dequeueReusableCellWithIdentifier:@"addbtn" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        //添加视图
        cell.addBlock=^(){
            [weakSelf addDataDefaine:self.num+1];
            //定位
            [self.tableView reloadData];
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArray.count-1 inSection:0]
                                  atScrollPosition:UITableViewScrollPositionTop animated:YES];
        };
        return cell;
    }else{//显示视图
        B_addWLCell_1 *cell=[tableView dequeueReusableCellWithIdentifier:@"addCell"];
        
        //第一行隐藏 删除按钮
        if(indexPath.row==0){
            cell.dleBtn.hidden=YES;
        }else{
            cell.dleBtn.hidden=NO;
        }
        
        //设置数据
        Site_04_01 *siteDetail=[self.dataArray objectAtIndex:indexPath.row];
        cell.site=siteDetail;
        
        //设置标题
        NSArray *numStr=[siteDetail.number componentsSeparatedByString:@"号"];
        self.num=[[numStr lastObject]integerValue];
        
        //设置序号
        cell.titLab.text=siteDetail.number;
        
        //车牌号
        cell.text1Block=^(NSString *str){
            siteDetail.chep=str;
            
        };
        
        //运输单号
        cell.text2Block=^(NSString *str){
            siteDetail.yuns=str;
            
        };
        
        //运输坍落度
        cell.text3Block=^(NSString *str){
            siteDetail.luod=str;
            
        };
        
        //出罐温度
        cell.text4Block=^(NSString *str){
            siteDetail.chug=str;
        };
        
        //资料是否安全
        cell.s1Block=^(UISwitch *sender){
            siteDetail.is_1=sender.on==0?@"否":@"是";
            
        };
        
        //是否合格
        cell.s2Block=^(UISwitch *sender){
            siteDetail.is_2=sender.on==0?@"否":@"是";
            
        };
        
        //备注
        cell.text5Block=^(NSString *value){
            siteDetail.beiz=value;
        };
        
        //删除
        cell.deleteBlock=^(){
            [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
            
            [weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
            [weakSelf.tableView reloadData];
        };
        return cell;
    }
}

#pragma mark scrollview
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        [self.view endEditing:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
